home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14211 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  81 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sizeof() question >>> :)
  5. Date: 12 Apr 1996 15:50:42 GMT
  6. Organization: OpenVision
  7. Message-ID: <4klu4i$7mc@spanky.pls.ov.com>
  8. References: <1996Apr12.061927@topaz>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 061927@topaz, naderr@topaz.cqu.edu.au writes:
  13. >Hi,
  14. >
  15. >How can I get, with a pointer,  the sizeof  of an  array that is
  16. >pointed by the pointer ?  (No it's not a tounge twister :)
  17. >
  18. >Ok, let see if with an example I  could clarify what I'm trying to say,
  19. >
  20. >
  21. >  char First_name[20];
  22. >  char Last_name[20];
  23. >  char Address[60];
  24. >  char Phone[20];
  25. >  char **ct, *cp;
  26. >  char *record[4];
  27. >  char ch;
  28. >  
  29. >  record[0] = First_name;
  30. >  record[1] = Last_name;
  31. >  record[2] = Address;
  32. >  record[3] = Phone;
  33. >  ct = record;
  34. >  cp = *ct;
  35. >
  36. >  while (!done) {
  37. >    ch = getchar();
  38. >    if (cp - *ct < sizeof(XXXXXXX)-1) {
  39. >      *cp++ = ch;
  40. >    } else {
  41. >      beep();
  42. >    }  
  43. >  }
  44. >
  45. >for XXXXXXX the _only_ thing that I can find that works is to stick 
  46. >in there is the actual array that cp is pointing to, i.e. sizeof(First_name).
  47. >
  48. >What ideas do you have so that I can get the size of the array currently 
  49. >pointed to by cp?
  50. >
  51. >Any ideas greatly appreciated,
  52. >
  53. >please email direct to me (as well if you wish to followup) at
  54. >
  55. >        naderr@topaz.cqu.edu.au
  56. >
  57. >TIA,
  58. >
  59. >        Rob 
  60. >
  61. >   
  62.  
  63.  
  64.  
  65. Sizeof is a compile time directive, and as such it cannot be used in a
  66. dynamic fashion at run time.  I believe that the proper solution to your
  67. problem is to create a struct definition like the one below:
  68.  
  69. struct record
  70. {
  71.     char *string;
  72.     int storage_allowed;
  73. };
  74.  
  75. Then you can use malloc() to provide the variable length storage
  76. for each of your arrays, and use the value of "storage_allowed" to
  77. act as the limit value in your input loop.
  78.  
  79.         Fletcher.Glenn@ov.com
  80.  
  81.